<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.4.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">


<link rel="stylesheet" href="/lib/font-awesome/css/all.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"vchao.top","root":"/","scheme":"Muse","version":"7.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":false,"show_result":false,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":false,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}}};
  </script>

  <meta name="description" content="思维导图是个好东西，可以把知识串在一张图上，更便于理解记忆。  概述Context是一个抽象基类。在翻译为上下文，也可以理解为环境，提供一些程序的运行环境基础信息 子类关系封装类ContextWrapper  ContextThemeWrapper（带主题的封装类） Activity Service Application    实现类ContextImpl 内存泄露问题 静态资源导致的内存泄漏">
<meta property="og:type" content="article">
<meta property="og:title" content="谈谈安卓中Context的理解[附思维导图]">
<meta property="og:url" content="http://vchao.top/2021/08/25/%E8%B0%88%E8%B0%88%E5%AE%89%E5%8D%93%E4%B8%ADContext%E7%9A%84%E7%90%86%E8%A7%A3[%E9%99%84%E6%80%9D%E7%BB%B4%E5%AF%BC%E5%9B%BE]/index.html">
<meta property="og:site_name" content="乘风破浪">
<meta property="og:description" content="思维导图是个好东西，可以把知识串在一张图上，更便于理解记忆。  概述Context是一个抽象基类。在翻译为上下文，也可以理解为环境，提供一些程序的运行环境基础信息 子类关系封装类ContextWrapper  ContextThemeWrapper（带主题的封装类） Activity Service Application    实现类ContextImpl 内存泄露问题 静态资源导致的内存泄漏">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://img-blog.csdnimg.cn/20190806225833838.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly92Y2hhby5ibG9nLmNzZG4ubmV0,size_16,color_FFFFFF,t_70">
<meta property="article:published_time" content="2021-08-25T14:42:51.261Z">
<meta property="article:modified_time" content="2021-08-25T14:37:20.817Z">
<meta property="article:author" content="vchao">
<meta property="article:tag" content="Android">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://img-blog.csdnimg.cn/20190806225833838.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly92Y2hhby5ibG9nLmNzZG4ubmV0,size_16,color_FFFFFF,t_70">

<link rel="canonical" href="http://vchao.top/2021/08/25/%E8%B0%88%E8%B0%88%E5%AE%89%E5%8D%93%E4%B8%ADContext%E7%9A%84%E7%90%86%E8%A7%A3[%E9%99%84%E6%80%9D%E7%BB%B4%E5%AF%BC%E5%9B%BE]/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>谈谈安卓中Context的理解[附思维导图] | 乘风破浪</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">乘风破浪</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a>

  </li>
  </ul>
</nav>




</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://vchao.top/2021/08/25/%E8%B0%88%E8%B0%88%E5%AE%89%E5%8D%93%E4%B8%ADContext%E7%9A%84%E7%90%86%E8%A7%A3[%E9%99%84%E6%80%9D%E7%BB%B4%E5%AF%BC%E5%9B%BE]/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="vchao">
      <meta itemprop="description" content="怀揣好奇，梦想改变世界的菜鸟极客">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="乘风破浪">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          谈谈安卓中Context的理解[附思维导图]
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>
              

              <time title="创建时间：2021-08-25 22:42:51 / 修改时间：22:37:20" itemprop="dateCreated datePublished" datetime="2021-08-25T22:42:51+08:00">2021-08-25</time>
            </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/Android/" itemprop="url" rel="index"><span itemprop="name">Android</span></a>
                </span>
            </span>

          

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <blockquote>
<p>思维导图是个好东西，可以把知识串在一张图上，更便于理解记忆。</p>
</blockquote>
<h2 id="概述"><a href="#概述" class="headerlink" title="概述"></a>概述</h2><p>Context是一个抽象基类。在翻译为上下文，也可以理解为环境，提供一些程序的运行环境基础信息</p>
<h2 id="子类关系"><a href="#子类关系" class="headerlink" title="子类关系"></a>子类关系</h2><p>封装类ContextWrapper</p>
<ul>
<li>ContextThemeWrapper（带主题的封装类）<ul>
<li>Activity</li>
<li>Service</li>
<li>Application</li>
</ul>
</li>
</ul>
<p>实现类ContextImpl</p>
<h2 id="内存泄露问题"><a href="#内存泄露问题" class="headerlink" title="内存泄露问题"></a>内存泄露问题</h2><ul>
<li>静态资源导致的内存泄漏<pre><code>  解决方案：避免使用静态资源，或者使用弱引用来解决相应。
</code></pre>
</li>
<li>单例模式导致内存泄漏<pre><code>  解决方案：使用getApplicationContext()，这样使用单例与Activity就无关了，Activity释放时则不会出现内存泄露
</code></pre>
<h2 id="获取Context"><a href="#获取Context" class="headerlink" title="获取Context"></a>获取Context</h2></li>
<li>View.getContext()<pre><code>  返回当前View对象的Context对象，通常是当前正在展示的Activity对象
</code></pre>
</li>
<li>Activity.getApplicationContext()<pre><code>  获取当前Activity所在应用进程的Context对象，通常使用Context时，优先考虑全局进程Context
</code></pre>
</li>
<li>ContextWrapper.getBaseContext()<pre><code>  获取一个ContextWrapper装饰之前的Context，在实际开发中使用并不多，不建议使用
</code></pre>
</li>
<li>Activity.this <pre><code>  返回当前Activity实例，UI控件需要使用Activity作为Context对象
</code></pre>
<h2 id="总结"><a href="#总结" class="headerlink" title="总结"></a>总结</h2></li>
<li>Context共有三种类型，Application、Activity和Service<pre><code>  整个App共一个Application对象
</code></pre>
</li>
<li>Context数量 = Activity数量 + Service数量 + 1(Application)</li>
<li>三个类都属于Context，功能是由ContextImpl类去实现的</li>
<li>大多数场景下，三种类型的Context可以通用</li>
<li>特殊场景不可通用（只能使用Activity类型的Context，否则将会出错）<ul>
<li>启动Activity<pre><code>  安全考虑，Android不允许Activity或Dialog凭空出现，Activity的启动必须要建立在另一个Activity的基础上，以形成返回栈
</code></pre>
</li>
<li>弹出Dialog<pre><code>  Dialog则必须在一个Activity上面弹出（除非是System Alert类型的Dialog）
</code></pre>
<h2 id="思维导图"><a href="#思维导图" class="headerlink" title="思维导图"></a>思维导图</h2><img src="https://img-blog.csdnimg.cn/20190806225833838.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly92Y2hhby5ibG9nLmNzZG4ubmV0,size_16,color_FFFFFF,t_70" alt="安卓Context的理解">    </li>
</ul>
</li>
</ul>

    </div>

    
    
    

      <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/Android/" rel="tag"># Android</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/2021/08/25/%E8%AE%B0%E4%B8%80%E6%AC%A1%E9%80%82%E9%85%8Dbug%E8%A7%A3%E5%86%B3%E8%BF%87%E7%A8%8B%EF%BC%8C%E6%9D%83%E7%94%A8%E4%BD%9C%E7%BB%8F%E9%AA%8C%E6%95%99%E8%AE%AD%E5%88%86%E4%BA%AB/" rel="prev" title="记一次适配bug解决过程，权用作经验教训分享">
      <i class="fa fa-chevron-left"></i> 记一次适配bug解决过程，权用作经验教训分享
    </a></div>
      <div class="post-nav-item"></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%A6%82%E8%BF%B0"><span class="nav-number">1.</span> <span class="nav-text">概述</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%AD%90%E7%B1%BB%E5%85%B3%E7%B3%BB"><span class="nav-number">2.</span> <span class="nav-text">子类关系</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%86%85%E5%AD%98%E6%B3%84%E9%9C%B2%E9%97%AE%E9%A2%98"><span class="nav-number">3.</span> <span class="nav-text">内存泄露问题</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E8%8E%B7%E5%8F%96Context"><span class="nav-number">4.</span> <span class="nav-text">获取Context</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%80%BB%E7%BB%93"><span class="nav-number">5.</span> <span class="nav-text">总结</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%80%9D%E7%BB%B4%E5%AF%BC%E5%9B%BE"><span class="nav-number">6.</span> <span class="nav-text">思维导图</span></a></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">vchao</p>
  <div class="site-description" itemprop="description">怀揣好奇，梦想改变世界的菜鸟极客</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">46</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
        <span class="site-state-item-count">4</span>
        <span class="site-state-item-name">分类</span>
      </div>
      <div class="site-state-item site-state-tags">
        <span class="site-state-item-count">27</span>
        <span class="site-state-item-name">标签</span>
      </div>
  </nav>
</div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        
  <div class="beian"><a href="https://beian.miit.gov.cn/" rel="noopener" target="_blank">冀ICP备 17022751号-1 </a>
      <img src="https://beian.miit.gov.cn" style="display: inline-block;">
  </div>

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2021</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">vchao</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://muse.theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Muse</a> 强力驱动
  </div>

        








      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/muse.js"></script>


<script src="/js/next-boot.js"></script>




  















  

  

</body>
</html>
